<template>
	<view>
		<!--搜索框 -->
		<view class="search_box">
			<uni-search-bar clearButton='none' cancelButton='none' @input="input($event)" radius="100" />
		</view>
		<!-- 搜索历史 -->
		<view class="history_box" v-if="searchList.length===0">
			<view class="history_title">
				<text>搜索历史</text>
				<uni-icons type="trash" size="17" @click="delehistory"></uni-icons>
			</view>
			<view class="history_tag">
				<uni-tag :text="item" type="" v-for="(item,index) in hist" :key="index" @click="gotlist(item)"></uni-tag>
			</view>
		</view>
		<!-- 搜索列表 -->
		<view class="sugg_list" v-else>
			<view class="sugg_item" v-for="(item,index) in searchList" :key="item.goods_id" @tap="gotgoods(item)">
				<view class="goods_name">
					{{item.goods_name}}
				</view>
				<uni-icons type="arrowright" size="16"></uni-icons>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 延时器标识
				timer: null,
				//关键字
				query: '',
				// 搜索结果
				searchList:[],
				// 搜索历史
				hidtorylist:[]
			};
		},
		onLoad() {
			this.hidtorylist=JSON.parse(uni.getStorageSync('query')||'[]')
		},
		methods: {
			// input框的事件处理函数
			input(e) {
				clearTimeout(this.timer)
				this.timer=setTimeout(()=>{
					this.query=e
					// console.log(this.query)
					this.getSearch()
				},500)
				
			},
			getSearch(){
				if(this.query!==''){
					this.request({
						url:`/public/v1/goods/qsearch`,
						data:{query:this.query}
					}).then(({data:res})=>{
						console.log(res)
						this.searchList=res.message
						this.searchli()
					})
					
				}else{
					this.searchList=[]
					return
				}
			},
			// 跳转详情页
			gotgoods(item){
				uni.navigateTo({
					url:`/subpkg/goods_detail/goods_detail?goods_id=${item.goods_id}`
				})
			},
			// 保存搜索历史
			searchli(){
				const set =new Set(this.hidtorylist)
				set.delete(this.query)
				set.add(this.query)
				this.hidtorylist=Array.from(set)
				console.log(this.hidtorylist)
				uni.setStorageSync('query',JSON.stringify(this.hidtorylist))
			},
			// 删除搜索历史
			delehistory(){
				this.hidtorylist=[]
				uni.removeStorageSync('query')
			},
			// 点击搜索历史标签跳转商品列表
			gotlist(item){
				uni.navigateTo({
					url:`/subpkg/goods_list/goods_list?query=${item}`
				})
			}
		},
		computed:{
			hist(){
				// 反转数组数据
				return [...this.hidtorylist].reverse()
			}
		}
	}
</script>

<style lang="scss">
	.search_box {
		position: sticky;
		top: 0;
		z-index: 9999;
	}
	.sugg_list{
		padding: 0 5px;
		.sugg_item{
			font-size: 12px;
			padding: 13px 0 ;
			border-bottom: 1px solid #efefef;
			display: flex;
			align-items: center;
			justify-content: space-between;
			.goods_name{
				// 文本不换行
				white-space: nowrap;
				overflow: hidden;
				// 文本溢出使用。。。代替
				text-overflow: ellipsis;
				margin-right: 3px;
				
			}
		}
	}
	.history_box{
		padding: 0 5px;
		.history_title{
			height: 40px;
			display: flex;
			justify-content: space-between;
			border-bottom: 1px solid #c00000;
			align-items: center;

			text{
				font-size: 13px;
			}
		}
		.history_tag{
			display: flex;
			flex-wrap:wrap;
		}
	}
	uni-tag{
		margin-top: 5px;
		margin-right: 5px;
	}
</style>
